window.onload = function(){

	var space = document.getElementById("space");
	var left = document.getElementById("left");
	var right = document.getElementById("right");

	var mu = document.getElementById("mu");
	var md = document.getElementById("md");
	var ml = document.getElementById("ml");
	var mr = document.getElementById("mr");

	var ck = document.getElementById("click");
	var dbck = document.getElementById("db-click");
	var rck = document.getElementById("r-click");

	var typing = document.getElementById("typing");
	var submit_typing = document.getElementById("submit-typing");

	var key = document.getElementById("key");
	var submit_key = document.getElementById("submit-key");

	var hotkey = document.getElementById("hotkey");
	var submit_hotkey = document.getElementById("submit-hotkey");

	// var esc = document.getElementById("esc");
	// var fullscr = document.getElementById("fullscr");

	var id1,id2,id3,id4;
	var s1,s2,s3,s4;

	space.onclick = function(e){sendinfo('key','space');}
	left.onclick = function(e){sendinfo('key','left');}
	right.onclick = function(e){sendinfo('key','right');}

	// mu.ontouchstart = function(e){id1=setInterval(function(){sendinfo('mouse','up')},100);console.log(id1)}
	// mu.ootouchend = function(e){console.log(id1+'    mouse up');clearInterval(id1)}
	mu.onclick = function(e){
		if(s1==0){
			id1 = setInterval(function(){sendinfo('mouse','up')},200);
			s1 = 1;
			mu.innerHTML = 'stop';
		}else{clearInterval(id1);s1 = 0;mu.innerHTML='mouse-up'}
	}
	md.onclick = function(e){
		if(s2==0){
			id2 = setInterval(function(){sendinfo('mouse','down')},200);
			s2 = 1;
			md.innerHTML = 'stop';
		}else{clearInterval(id2);s2 = 0;md.innerHTML='mouse-down'}
	}
	ml.onclick = function(e){
		if(s3==0){
			id3 = setInterval(function(){sendinfo('mouse','left')},200);
			s3 = 1;
			ml.innerHTML = 'stop';
		}else{clearInterval(id3);s3 = 0;ml.innerHTML='mouse-left'}
	}	
	mr.onclick = function(e){
		if(s4==0){
			id4 = setInterval(function(){sendinfo('mouse','right')},200);
			s4 = 1;
			mr.innerHTML = 'stop';
		}else{clearInterval(id4);s4 = 0;mr.innerHTML='mouse-right'}
	}		
		
	// mu.onclick = function(e){sendinfo('mouse','up');}
	// md.onclick = function(e){sendinfo('mouse','down');}
	// ml.onclick = function(e){sendinfo('mouse','left');}
	// mr.onclick = function(e){sendinfo('mouse','right');}

	ck.onclick = function(e){sendinfo('mouse','click');}
	dbck.onclick = function(e){sendinfo('mouse','db-click');}
	rck.onclick = function(e){sendinfo('mouse','r-click');}

	submit_typing.onclick = function(e){
		sendinfo('key',typing.value);
		typing.value = '';
	}

	submit_key.onclick = function(e){
		sendinfo('key',key.value);
		key.value = '';
	}

	submit_hotkey.onclick = function(e){
		sendinfo('hotkey',hotkey.value);
		hotkey.value = '';
	}
	// esc.onclick = function(e){sendinfo('key','esc');}
	// fullscr.onclick = function(e){sendinfo('op','fullscr');}


	var SS = document.getElementById('SS');

	SS.onclick = function(e){
		var pagex = e.pageX-SS.offsetLeft;
		var pagey = e.pageY-SS.offsetTop;
		// alert('x:'+pagex+'y:'+pagey);
		sendinfo('mouse','x-'+pagex+'-y-'+pagey)
	}

	function sendinfo(key,value){
		var xhr = new XMLHttpRequest();
		xhr.open('POST','/ctrl',true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send(key+'='+value);
		
	}

	
}




















